const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');
var WxParse = require('../../common/wxParse/wxParse.js');
Page({
  data: {
    postArr:[],
    title:['','广告','资讯','活动'],
    page: {//话题详情
      p: 0,
      psize: 10
    },
    hei: 0,
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '发现'
    })
    this.init();
    wx.getSystemInfo({
      success: res => {
        let windowHeight = (res.windowHeight * (750 / res.windowWidth));
        this.setData({
          hei: windowHeight- 100
        })
      }
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  down() {
    wx.showLoading({
      title: '加载中',
      success: () => {
        this.setData({
          'page.p': this.data.page.p + 1
        })
        this.init()

      }
    })
  },
  imgYu(e) {
    let list = [],
      img = e.currentTarget.dataset.img;
      list.push(e.currentTarget.dataset.list[0])
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  up() {
    let refresh = true
    this.setData({
      'page.p': 0
    })
    this.init(refresh)
  },
  init(refresh = false){
    var that = this;
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Post/getList',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'p': this.data.page.p,
            'psize': this.data.page.psize,
            'is_system':1
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            if (res.data.success == '1') {
              if (res.data.data.list.length == 0){
                wx.showToast({
                  title: '暂无更多数据',
                  icon: 'none',
                  duration: 2000
                })
                return
              }
              let arr = refresh ? res.data.data.list : this.data.postArr.concat(res.data.data.list)
              for (let i = 0; i < arr.length; i++) {
                WxParse.wxParse('reply' + i, 'html', arr[i].content, that);
                if (i === arr.length - 1) {
                  WxParse.wxParseTemArray("topicArrs", 'reply', arr.length, that)
                }
                //WxParse.wxParseTemArray("topicArr", 'reply', val.topic_content, that)
              }
              this.setData({
                postArr: arr
              })
            }else{
              wx.showToast({
                title: '暂无更多数据',
                icon: 'none',
                duration: 2000
              })
            }
            wx.hideLoading();
            refresh == true && wx.stopPullDownRefresh()
          }
        })
      }
    })
    
  },
  handleZan(e){//点赞
    let post_id = e.currentTarget.dataset.postid;
    let idx = e.currentTarget.dataset.idx;
    util.zan({
      'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
      'post_id': post_id
    }, (res) => {
      let is_zan = 'postArr['+idx+'].is_zan';
      let zan_num = 'postArr['+idx+'].zan_num';
      let num = Number(this.data.postArr[idx].zan_num);
      this.setData({
        [is_zan]: this.data.postArr[idx].is_zan == 0 ? 1 : 0,
        [zan_num]: this.data.postArr[idx].is_zan == 0 ? num + 1 : num - 1
      })
    })
  },
  detail(e){
    wx.navigateTo({
      url: '../details/details?id=' + e.currentTarget.dataset.postid
    })
  }
})
